/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.auth.sys.api;

import com.arisgo.cloud.auth.sys.service.TenantFunctionService;
import com.arisgo.cloud.auth.sys.service.TenantService;
import com.arisgo.cloud.auth.sys.vo.TenantFunctionVo;
import com.arisgo.cloud.auth.sys.vo.TenantVo;
import com.arisgo.cloud.common.Pagination;
import com.arisgo.cloud.common.Result;
import com.arisgo.cloud.web.param.Paging;
import com.arisgo.cloud.web.param.Search;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 租户 Api
 *
 * @author Arisgo
 * @since 2024-06-14
 */
@RestController
@AllArgsConstructor
@RequestMapping("/tenant")
@Tag(name = "租户 API", description = "租户CRUD操作")
public class TenantApi {

    private final TenantService tenantService;

    private final TenantFunctionService tenantFunctionService;

    /**
     * 依id查询租户
     */
    @GetMapping(value = "/{id}")
    @Operation(summary = "依id查询")
    public Result<TenantVo> findById(@PathVariable(value = "id") Long id) {
        return Result.success(tenantService.findById(id));
    }

    /**
     * 分页查询租户
     */
    @PostMapping(value = "/page")
    @Operation(summary = "分页查询")
    public Result<Pagination<TenantVo>> page(@RequestBody Paging params) {
        return Result.success(tenantService.page(params));
    }

    /**
     * 条件查询租户
     */
    @PostMapping(value = "/find")
    @Operation(summary = "条件查询")
    public Result<List<TenantVo>> find(@RequestBody Search param) {
        return Result.success(tenantService.findByParams(param));
    }

    /**
     * 保存租户
     */
    @PostMapping
    @Operation(summary = "新增数据")
    public Result<TenantVo> save(@RequestBody TenantVo vo) {
        return Result.success(tenantService.save(vo));
    }

    /**
     * 更新租户
     */
    @PutMapping(value = "/{id}")
    @Operation(summary = "更新数据")
    public Result<Boolean> update(@PathVariable(value = "id") Long id, @RequestBody TenantVo vo) {
        vo.setId(id);
        return Result.success(tenantService.update(vo));
    }

    /**
     * 逻辑删除租户
     */
    @DeleteMapping(value = "/{id}")
    @Operation(summary = "单个逻辑删除")
    public Result<Boolean> delete(@PathVariable(value = "id") Long id) {
        return Result.success(tenantService.delete(id));
    }

    /**
     * 逻辑删除租户
     */
    @DeleteMapping
    @Operation(summary = "批量逻辑删除")
    public Result<Boolean> delete(@RequestBody List<Long> ids) {
        return Result.success(tenantService.delete(ids));
    }

    /**
     * 物理删除租户
     */
    @DeleteMapping(value = "/remove/{id}")
    @Operation(summary = "单个物理删除")
    public Result<Boolean> remove(@PathVariable(value = "id") Long id) {
        return Result.success(tenantService.remove(id));
    }

    /**
     * 物理删除租户
     */
    @DeleteMapping(value = "/remove")
    @Operation(summary = "批量物理删除")
    public Result<Boolean> remove(@RequestBody List<Long> ids) {
        return Result.success(tenantService.remove(ids));
    }


    @PostMapping(value = "/{tenantId}/function/page")
    @Operation(summary = "查询租户下的功能包")
    public Result<Pagination<TenantFunctionVo>> pageFunction(@PathVariable(value = "tenantId") Long tenantId,
                                                             @RequestBody Paging paging) {
        paging.addTenantIdParam(tenantId);
        return Result.success(tenantFunctionService.pageFunctionByTenantId(paging));
    }

    @PostMapping(value = "/{tenantId}/function")
    @Operation(summary = "保存")
    public Result<Boolean> saveFunction(@PathVariable(value = "tenantId") Long tenantId,
                                        @RequestBody TenantFunctionVo vo) {
        vo.setTenantId(tenantId);
        tenantFunctionService.save(vo);
        return Result.success(true);
    }

    @DeleteMapping(value = "/{tenantId}/function")
    @Operation(summary = "批量删除")
    public Result<Boolean> removeFunction(@PathVariable(value = "tenantId") Long tenantId,
                                          @RequestBody List<Long> ids) {
        tenantFunctionService.delete(ids);
        return Result.success(true);
    }

    @Operation(summary = "创建租户")
    @PostMapping(value = "/create")
    public Result<Map<Object, Object>> create(@RequestBody TenantVo vo) {
        return Result.success(tenantService.create(vo));
    }

    @Operation(summary = "切换租户")
    @GetMapping(value = "/toggle/{tenantId}")
    public Result<Map<Object, Object>> toggle(@PathVariable(value = "tenantId") Long tenantId) {
        return Result.success(tenantService.toggle(tenantId));
    }

    @Operation(summary = "注销租户")
    @DeleteMapping(value = "/logout/{tenantId}")
    public Result<Boolean> cancel(@PathVariable(value = "tenantId") Long tenantId) {
        return Result.success(tenantService.logout(tenantId));
    }

    @Operation(summary = "更换负责人")
    @PutMapping(value = "/change/{userId}")
    public Result<Map<Object, Object>> cancel(@PathVariable(value = "userId") Long userId,
                                              @RequestParam(value = "tenantId") Long tenantId) {
        return Result.success(tenantService.changeManager(tenantId, userId));
    }

}
